package com.kim.rssreader.service;

import com.kim.rssreader.utils.NetUtil;
import com.kim.rssreader.utils.StarterImageFetchUtils;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class StarterImageFetchService extends Service {

	/**
	 * 启动action
	 */
	public static final String ACTION_FETCH = "com.kim.rssreader.fetchimage";

	public interface ImageConfFetchCompleteCallback {
		void onImageConfComplete();
	}

	public interface ImageDataFecthCompleteCallback {
		void onImageDataComplete();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (NetUtil.isNetAvailable(this)) {
			final ImageDataFecthCompleteCallback imageDataFecthCompleteCallback = new ImageDataFecthCompleteCallback() {

				@Override
				public void onImageDataComplete() {
					stopSelf();
				}
			};
			final ImageConfFetchCompleteCallback callback = new ImageConfFetchCompleteCallback() {
				@Override
				public void onImageConfComplete() {
					StarterImageFetchUtils
							.fetchImageData(
									getApplicationContext(),
									StarterImageFetchUtils
											.getLocalImageVersion(getApplicationContext()),
									imageDataFecthCompleteCallback);
				}
			};
			StarterImageFetchUtils.fetchImage(this, callback);
		} else {
			stopSelf();
		}
		return super.onStartCommand(intent, flags, startId);
	}
}
